
import { Intersection } from "../../cccframework/Common/Components/Intersection";
import { gameHelper } from "../../cccframework/Utils/GameHelpler";
import { Line } from "./Line";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Geometry extends cc.Component {
    @property(cc.Node)
    p0: cc.Node = null;
    @property(cc.Node)
    p1: cc.Node = null;
    @property(cc.Node)
    p2: cc.Node = null;
    @property(cc.Node)
    p3: cc.Node = null;

    onLoad() {
        [
            this.p0,
            this.p1,
            this.p2,
            this.p3,
        ].forEach(v => {
            v.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        })
    }

    touchMove(touch: cc.Event.EventTouch) {
        let node = touch.target;
        node.worldPosition = touch.getLocation();
        console.log('move')
    }

    public drawLine(p0, p1) {
        let ctx = gameHelper.getDebugSceneCtx();
        // ctx.clear();
        ctx.moveTo(p0.x, p0.y);
        ctx.lineTo(p1.x, p1.y);
        ctx.stroke();
    }


    update(dt) {
        let ctx = gameHelper.getDebugSceneCtx();
        ctx.clear();
        if (Intersection.isIntersect(this.p0.position, this.p1.position, this.p2.position, this.p3.position)) {
            ctx.strokeColor = cc.color(255, 0, 0);
            let pos = Intersection.lineLine(this.p0.position, this.p1.position, this.p2.position, this.p3.position);
            // console.log('pos', pos)
            ctx.circle(pos.x, pos.y, 10);
        } else {
            ctx.strokeColor = cc.color(255, 255, 0);
        }
        this.drawLine(this.p0.position, this.p1.position);
        this.drawLine(this.p2.position, this.p3.position);
    }
}
